package com.etu.upload;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;

/**
 * 
 * 简单的实现HttpServletRequest的封装
 * 
 * */
public class RequestContext implements RequestContextImpl {
	private HttpServletRequest request;

	private static final String MULTIPART = "multipart/";
	
	public RequestContext(HttpServletRequest request){
		this.request=request;
	}
	public String getCharacterEncoding() {
		return request.getCharacterEncoding();
	}

	public int getContentLength() {
		return request.getContentLength();
	}

	public String getContentType() {
		return request.getContentType();
	}

	public InputStream getInputStream() throws IOException {
		return request.getInputStream();
	}
	public byte[] getBoundary() {
		String contentType=getContentType();
		int p=contentType.indexOf('=');
		contentType= contentType.substring(p+1);
		byte[] b=null;
		try {
			b= contentType.getBytes("UTF-8");
		} catch (UnsupportedEncodingException e) {
		
			e.printStackTrace();
		}
		return b;
	}
	public boolean isCanUpload() {		
		return isCanUpload(request);
	}
	/**
	 * @param HttpServletRequest request
	 * @return boolean if is MultipartContent
	 * */
    public static final boolean isCanUpload(
            HttpServletRequest request) {
        if (!"post".equals(request.getMethod().toLowerCase())) {
            return false;
        }
        String contentType = request.getContentType();
        if (contentType == null) {
            return false;
        }
        if (contentType.toLowerCase().startsWith(MULTIPART)) {
            return true;
        }
        return false;
    }
	public void checkSize(long size) {
		long MaxSize=100000;
		if(size>MaxSize){
			
		}
	}
	public void checkType(String Type) {
		// TODO Auto-generated method stub
		
	}
}
